package com.zhou.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.framework.dao.Log;
import com.zhou.framework.util.MapQueryBuilder;
import com.zhou.org.dto.LogQueryDto;
import com.zhou.org.mapper.LogMapper;
import com.zhou.org.service.LogService;
import com.zhou.util.DateUtil;
import com.zhou.util.StringTool;
import com.zhou.util.TimeUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lang.zhou
 * @since 2022-08-23
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Override
    public void saveLog(Log l) {
        save(l);
    }

    @Override
    public List<Log> listInfo(LogQueryDto dto) {
        LambdaQueryWrapper<Log> w = infoQuery();
        if(dto.getStatus() != null){
            w.eq(Log::getStatus,dto.getStatus());
        }
        if(dto.getLogLevel() != null){
            w.eq(Log::getLogLevel,dto.getLogLevel());
        }
        if(StringTool.isNotBlank(dto.getMethod())){
            w.eq(Log::getMethod,dto.getMethod());
        }
        if(StringTool.isNotBlank(dto.getIp())){
            w.eq(Log::getIp,dto.getIp());
        }
        if(StringTool.isNotBlank(dto.getUserName())){
            w.eq(Log::getUsername,dto.getUserName());
        }
        if(StringTool.isNotBlank(dto.getDescription())){
            w.like(Log::getDescription, "%" + dto.getDescription() + "%");
        }
        w.orderByDesc(Log::getCreateDt);
        return list(w);
    }

    @Override
    public void deleteLogBeforeDate(Date lastDt) {
        LambdaQueryWrapper<Log> w = new LambdaQueryWrapper<>();
        w.lt(Log::getCreateDt,lastDt);
        remove(w);
    }

    @Override
    public List<Log> getErrorLog(Date from, Date to) {
        LambdaQueryWrapper<Log> w = new LambdaQueryWrapper<>();
        w.ne(Log::getStatus,"Success");
        w.ge(Log::getCreateDt,from);
        w.le(Log::getCreateDt,to);
        return list(w);
    }

    private LambdaQueryWrapper<Log> infoQuery(){
        LambdaQueryWrapper<Log> w = new LambdaQueryWrapper<>();
        w.select(Log::getId,Log::getDescription,Log::getCreateDt,Log::getIp,Log::getLocation,Log::getLogLevel,Log::getMethod,Log::getStatus,Log::getSpendTime
                ,Log::getUsername,Log::getBrowser,Log::getOs,Log::getUrl);
        return w;
    }


    @Override
    public TreeMap<String, Long> getSevenCount() {
        Date now = new Date();
        Date to = TimeUtil.parseDate(DateUtil.getLastDay(now,0),"yyyyMMdd");
        Date from = TimeUtil.addHour(to,-6*24);
        LambdaQueryWrapper<Log> w = new LambdaQueryWrapper<>();
        w.select(Log::getCreateDt);

        w.ge(Log::getCreateDt,from);
        w.lt(Log::getCreateDt,to);
        List<Log> list = list(w);
        TreeMap<String, Long> cntMap = list.stream().map(o -> TimeUtil.formatDate(o.getCreateDt(), "yyyy-MM-dd")).collect(
                Collectors.groupingBy(a -> a, TreeMap::new, Collectors.counting())
        );

        return cntMap;
    }

    @Override
    public Map<String, Long> getAccessStatusCount() {
        Date to = TimeUtil.parseDate(DateUtil.getLastDay(new Date(),0),"yyyyMMdd");
        Date from = TimeUtil.addHour(to,-6*24);
        LambdaQueryWrapper<Log> w = new LambdaQueryWrapper<>();
        w.select(Log::getStatus);

        w.ge(Log::getCreateDt,from);
        w.lt(Log::getCreateDt,to);
        List<Log> list = list(w);
        TreeMap<String, Long> cntMap = list.stream().map(Log::getStatus).collect(
                Collectors.groupingBy(a -> a, TreeMap::new, Collectors.counting())
        );

        return cntMap;
    }
}
